package com.safeneeds.linguimana.web;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.safeneeds.linguimana.dto.DataStorePort;
import com.safeneeds.linguimana.entity.EntityPortNews;
import com.safeneeds.linguimana.entity.PortNews;
import com.safeneeds.linguimana.entity.PortNewsCate;
import com.safeneeds.linguimana.entity.PortNewsCateMap;
import com.safeneeds.linguimana.ser.IBgNewsCateService;
import com.safeneeds.linguimana.ser.IBgNewsService;
import com.safeneeds.util.base.BaseAction;
import com.safeneeds.util.base.IUserInfo;
import com.safeneeds.util.base.RetType;
import com.safeneeds.util.other.StringUtil;
import com.safeneeds.util.page.PageInfo;

/**
 * 门户新闻管理
 * @author gaof
 * @date 2017年2月8日
 */
@Controller
@RequestMapping("bgPort")
public class BgNewsAction extends BaseAction{
	
	@Resource(name="bgNewsService")
	private IBgNewsService bgNewsService;
	
	@Resource(name="bgNewsCateService")
	private IBgNewsCateService bgNewsCateService;

	/**
	 * 获取某栏目新闻分页数据
	 * @param modelMap
	 * @param pageInfo
	 * @param dataStore
	 * @return
	 */
	@RequestMapping("listNews.do")
	public String listNews(ModelMap modelMap, PageInfo pageInfo, DataStorePort dataStore){
		
		if(dataStore != null && !StringUtil.isEmpty(dataStore.getMainCateId())){
			System.out.println(dataStore.toString());
			//获取栏目信息
			PortNewsCate cate = bgNewsCateService.getNewsCateById(dataStore.getMainCateId());
			if(cate == null){
				return getViewsModelPathSysError();
			}
			
			//获取栏目下所有分类
			List<PortNewsCate> cateList = new ArrayList<PortNewsCate>();
			bgNewsCateService.getAllChildCate(cate, cateList);
			
			//获取分页数据
			pageInfo = bgNewsService.listNewsPage(pageInfo, dataStore);
			
			modelMap.put("pageInfo", pageInfo);
			modelMap.put("dataStore", dataStore);
			modelMap.put("cate", cate);
			modelMap.put("cateList", cateList);
		}
		
		return getViewsModelPathSys("portmana/bg/news/newsList"); 
	}
	
	/**
	 * 新增/编辑新闻登录
	 * @param modelMap
	 * @param news
	 * @param mainCateId 新闻主分类id
	 * @return
	 */
	@RequestMapping("addNewsLogin.do")
	public String addNewsLogin(ModelMap modelMap, PortNews news, DataStorePort dataStore){
		//获取栏目信息
		PortNewsCate cate = bgNewsCateService.getNewsCateById(dataStore.getMainCateId());
		if(cate == null){
			return getViewsModelPathSysError();
		}
		
		//获取栏目下所有分类
		List<PortNewsCate> cateList = new ArrayList<PortNewsCate>();
		bgNewsCateService.getAllChildCate(cate, cateList);
		
		modelMap.put("cate", cate);
		modelMap.put("cateList", cateList);
		modelMap.put("dataStore", dataStore);
		
		if(news != null && !StringUtil.isEmpty(news.getNewsId())){
			//获取新闻信息
			news = bgNewsService.getNewsById(news.getNewsId());
			
			//获取新闻对应分类
			List<PortNewsCateMap> cateMapList = bgNewsCateService.getNewsCateByNewsId(news.getNewsId());
			String cateMap = "";
			for (int i = 0; i < cateMapList.size(); i++) {
				cateMap += cateMapList.get(i).getId().getCateId() + ",";
			}
			if(!StringUtil.isEmpty(cateMap)){
				cateMap = cateMap.substring(0, cateMap.lastIndexOf(","));
			}
			
			modelMap.put("news", news);
			modelMap.put("cateMap", cateMap);
		}
		
		return getViewsModelPathSys("portmana/bg/news/editNews");
	}
	
	/**
	 * 添加、更新新闻
	 * @param anno
	 * @return
	 */
	@RequestMapping("addNews.do")
	public String addNews(HttpServletRequest request, ModelMap modelMap, PortNews news, DataStorePort dataStore, RedirectAttributes attr){
		IUserInfo userInfo = getUserInfo(request);
		bgNewsService.addOrEditNews(news, dataStore, userInfo);
	
		attr.addAttribute("mainCateId", dataStore.getMainCateId());
		return "redirect:/bgPort/listNews.do";
	}
	
	/**
	 * 新闻详情
	 * @param modelMap
	 * @param news
	 * @return
	 */
	@RequestMapping("newsDetail.do")
	public String addNewsLogin(ModelMap modelMap, PortNews news){
		
		//获取新闻信息
		news = bgNewsService.getNewsById(news.getNewsId());
		//获取新闻对应分类信息
		List cateList = bgNewsCateService.getNewsCateMapNameList(news.getNewsId());
		
		modelMap.put("news", news);
		modelMap.put("cateList", cateList);
		
		return getViewsModelPathSys("portmana/bg/news/newsDetail");
	}
	
	@ResponseBody
	@RequestMapping("updateNewIsShow.ido")
	public RetType updateNewIsShow(String ids,String isShow){
		//System.out.println(nameIsShow);
		System.out.println(ids+"mmm新闻-隐藏或发布mmmm"+isShow);
		RetType reType = new RetType();
		int temp = bgNewsService.updateNewShow(ids,isShow);
		if(temp>0){
			System.out.println("temp+++++++++");
			reType.setErrCode("1");
		}else{
			reType.setErrCode("2");
		}
		return reType;
	} 
}
